package com.mall.users.service;

import com.mall.commons.exception.BusinessException;
import com.mall.model.domain.UmsMenu;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.MenuVo;
import com.mall.users.mapper.UmsMenuMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsMenuService {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    /**
     * 根据菜单id查询对象
     * @param menuIds
     * @return
     */
    public List<UmsMenu> findByIds(List<Long> menuIds) {
        return umsMenuMapper.selectByIds(menuIds);
    }

    /**
     * 根据id查询菜单信息
     * @param ids
     * @return
     */
    public List<UmsMenu> selectByIds(List<Long> ids) {
        return umsMenuMapper.selectByIds(ids);
    }


    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    public List<MenuVo> treeList() {
        List<UmsMenu> entities = umsMenuMapper.selectAll();

        //查询所有所有列表转化为vo
        List<MenuVo> menuVos = entities.stream().map(item -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(item, menuVo);
            return menuVo;
        }).collect(Collectors.toList());


        List<MenuVo> childrenList = menuVos.stream().filter(item ->
                item.getParentId() == 0
        ).map((menu)->{
            menu.setChildren(getChildrenData(menu,menuVos));
            return menu;
        }).collect(Collectors.toList());
        return childrenList;
    }

    //递归获取子菜单
    private List<MenuVo> getChildrenData(MenuVo root, List<MenuVo> all) {
        List<MenuVo> children = all.stream().filter(menuVo ->
                menuVo.getParentId() == root.getId()
        ).map(menuVo -> {
            menuVo.setChildren(getChildrenData(menuVo,all));
            return menuVo;
        }).collect(Collectors.toList());
        return children;
    }

    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public CommonPage list(Long parentId, Integer pageNum, Integer pageSize) {
        Integer count = umsMenuMapper.selectCount(parentId);
        List<UmsMenu> list = umsMenuMapper.selectByPage(parentId, pageNum, pageSize);
        return new CommonPage(pageNum, pageSize, count, list);
    }

    /**
     * 新增菜单
     * @param umsMenu
     */
    public void create(UmsMenu umsMenu) {
        UmsMenu menu = umsMenuMapper.selectByTitle(umsMenu.getTitle());
        if (menu!= null) {
            throw new BusinessException("菜单名称已存在");
        }
        UmsMenu menu2 = umsMenuMapper.selectByName(umsMenu.getName());
        if (menu2!= null) {
            throw new BusinessException("前端名称已存在");
        }
        umsMenu.setCreateTime(new Date());
        //设置一级菜单还是二级菜单(父id为0就是一级菜单)
        umsMenu.setLevel(umsMenu.getParentId() == 0L? 0:1);
        umsMenuMapper.save(umsMenu);
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     */
    public void delete(Long id) {
        umsMenuMapper.deleteById(id);
    }

    public UmsMenu getById(Long id) {
        return umsMenuMapper.selectById(id);
    }

    /**
     * 修改菜单
     * @param umsMenu
     */
    public void update(UmsMenu umsMenu) {
        UmsMenu menu = umsMenuMapper.selectByTitle(umsMenu.getTitle());
        if (menu!= null && !menu.getTitle().equals(umsMenu.getTitle())) {
            throw new BusinessException("菜单名称已存在");
        }
        UmsMenu menu2 = umsMenuMapper.selectByName(umsMenu.getName());
        if (menu2!= null && !menu2.getName().equals(umsMenu.getName())) {
            throw new BusinessException("前端名称已存在");
        }
        umsMenu.setLevel(umsMenu.getParentId() == 0L? 0 : 1);
        umsMenuMapper.update(umsMenu);
    }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     */
    public void updateHidden(Long id, Integer hidden) {
        umsMenuMapper.updateHidden(id, hidden);
    }


}
